﻿using System;
using System.Reflection;
using System.Web;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using AnyOffice.Components;
using log4net;
namespace AnyOffice.general.file_folder
{
    public partial class newfile : ModulePage
    {
        private static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                string str = (Request.QueryString["parent_folder"] != null) ? Request.QueryString["parent_folder"].ToString() : "/";
                this.ViewState["ParentFolder"] = str;
                this.parentPath.DataSource = new LiteUtility().GetDtbDirectory();
                this.parentPath.DataTextField = "path";
                this.parentPath.DataValueField = "path";
                this.parentPath.DataBind();
                if (this.parentPath.Items.FindByValue(str) != null)
                {
                    this.parentPath.Items.FindByValue(str).Selected = true;
                }
                this.btnSubmit.Attributes.Add("onclick", "return CheckForm();");
            }

        }
        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("index.aspx?path=" + Server.UrlEncode(this.ViewState["ParentFolder"].ToString()));
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            this.pnlMain.Visible = false;
            string path = "/attachment/file_folder/" + HttpContext.Current.User.Identity.Name + this.parentPath.Value;
            if (!path.EndsWith("/"))
            {
                path = path + "/";
            }
            UserCache cache = new UserCache();
            long num = new LiteUtility().UsedSize();
            long num2 = ((cache.FolderCapacity * 1024) * 1024) - num;
            long num3 = 0L;
            if (this.File1.PostedFile.ContentLength > 0)
            {
                num3 += this.File1.PostedFile.ContentLength;
            }
            if (this.File2.PostedFile.ContentLength > 0)
            {
                num3 += this.File2.PostedFile.ContentLength;
            }
            if (this.File3.PostedFile.ContentLength > 0)
            {
                num3 += this.File3.PostedFile.ContentLength;
            }
            if ((num3 > num2) && (cache.FolderCapacity != 0))
            {
                this.pnlMain.Visible = false;
                this.litMessage.Text = base.ShowPrompt("没有足够的空间，上传文件失败！", "index.aspx?path=" + Server.UrlEncode(this.ViewState["ParentFolder"].ToString()));
            }
            else
            {
                try
                {
                    Upload upload = new Upload();
                    upload.AutoCreateDirectory = true;
                    upload.OverwriteFile = true;
                    upload.DestinationPath = Server.MapPath(path);
                    if (this.File1.PostedFile.ContentLength > 0)
                    {
                        upload.GetFile(this.File1.PostedFile);
                        upload.DoUpload();
                    }
                    if (this.File2.PostedFile.ContentLength > 0)
                    {
                        upload.GetFile(this.File2.PostedFile);
                        upload.DoUpload();
                    }
                    if (this.File3.PostedFile.ContentLength > 0)
                    {
                        upload.GetFile(this.File3.PostedFile);
                        upload.DoUpload();
                    }
                }
                catch (Exception exception)
                {
                    if (log.IsErrorEnabled)
                    {
                        log.Error("", exception);
                    }
                    this.litMessage.Text = base.ShowPrompt("上传文件失败！", "index.aspx?path=" + Server.UrlEncode(this.ViewState["ParentFolder"].ToString()));
                    return;
                }
                Response.Redirect("index.aspx?path=" + Server.UrlEncode(this.ViewState["ParentFolder"].ToString()));
            }
        }
    }
}
